package com.example.aussiehealthhelper;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.appbar.MaterialToolbar;

public class HealthNoteEditActivity extends AppCompatActivity {

    private HealthNotesDbHelper dbHelper;
    private EditText etTitle, etContent;
    private long noteId = -1; // -1表示新笔记

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health_note_edit);

        dbHelper = new HealthNotesDbHelper(this);
        etTitle = findViewById(R.id.etNoteTitle);
        etContent = findViewById(R.id.etNoteContent);

        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(v -> onBackPressed());

        //检查是否是编辑现有笔记
        noteId = getIntent().getLongExtra("note_id", -1);
        if (noteId != -1) {
            loadNoteData();
        }

        //保存按钮
        findViewById(R.id.btnSaveNote).setOnClickListener(v -> saveNote());

        //删除按钮
        View btnDelete = findViewById(R.id.btnDeleteNote);
        btnDelete.setVisibility(noteId == -1 ? View.GONE : View.VISIBLE);
        btnDelete.setOnClickListener(v -> showDeleteConfirmation());
    }

    private void loadNoteData() {
        HealthNote note = dbHelper.getNoteById(noteId);
        if (note != null) {
            etTitle.setText(note.getTitle());
            etContent.setText(note.getContent());
        }
    }

    private void saveNote() {
        String title = etTitle.getText().toString().trim();
        String content = etContent.getText().toString().trim();

        if (TextUtils.isEmpty(title)) {
            Toast.makeText(this, "请输入笔记标题", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(content)) {
            Toast.makeText(this, "请输入笔记内容", Toast.LENGTH_SHORT).show();
            return;
        }

        if (noteId == -1) {
            //创建新笔记
            long newId = dbHelper.addNote(title, content);
            if (newId != -1) {
                Toast.makeText(this, "笔记保存成功", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "保存失败，请重试", Toast.LENGTH_SHORT).show();
            }
        } else {
            //更新现有笔记
            if (dbHelper.updateNote(noteId, title, content)) {
                Toast.makeText(this, "笔记更新成功", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "更新失败，请重试", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void showDeleteConfirmation() {
        new AlertDialog.Builder(this)
                .setTitle("删除笔记")
                .setMessage("确定要删除此笔记吗？")
                .setPositiveButton("删除", (dialog, which) -> deleteNote())
                .setNegativeButton("取消", null)
                .show();
    }

    private void deleteNote() {
        if (dbHelper.deleteNote(noteId)) {
            Toast.makeText(this, "笔记已删除", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText(this, "删除失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }
}